package com.demo.controller;

import com.demo.bean.DailyActivity;
import com.demo.bean.User;
import com.demo.service.DailyActivityService;
import com.demo.service.RedisService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author 李天航
 * 操作日常活动的接口
 */
@Api(tags = "日常活动")
@RestController
@RequestMapping("/dailyactivity")
public class DailyActivityController {

    @Resource
    DailyActivityService dailyActivityService;
    @Resource
    RedisService redisService;

    /**
     * 获取用户账号对应的日常活动
     * @param session 缓存 获取用户账号
     * @return dailyActivities:日常活动表 status:操作状态
     */
    @PostMapping("/getall")
    public LinkedHashMap<String,Object> getDailyActivitiesByUserAccount(HttpSession session)
    {
        User user=(User)  redisService.getSessionData(session.getId(),"user");
        return dailyActivityService.getDailyActivitiesByUserAccount(user.getUserAccount());
    }

    /**
     * 添加一项日常活动
     * @param dailyActivity 用户账号信息
     * @return status,msg,如果有冲突会有addDailyActivity和ConflictDailyActivity
     */
    @PostMapping("/add")
    public LinkedHashMap<String,Object> addDailyActivity(DailyActivity dailyActivity,HttpSession session)
    {
        User user=(User)  redisService.getSessionData(session.getId(),"user");
        dailyActivity.setUserAccount(user.getUserAccount());
        return dailyActivityService.addDailyActivity(dailyActivity);
    }

    /**
     * 通过主键删除一项日常活动
     * @param dailyId 日常活动的id
     * @return status: 操作状态
     */
    @PostMapping("/delete")
    public LinkedHashMap<String,Object> deleteDailyActivity(int dailyId,HttpSession session)
    {
        User user=(User)  redisService.getSessionData(session.getId(),"user");
        return dailyActivityService.deleteDailyActivity(dailyId,user.getUserAccount());
    }

    /**
     * 更新一项日常活动
     * @param dailyActivity 更新后的信息
     * @return status,msg,如果有冲突会有addDailyActivity和ConflictDailyActivity
     */
    @PostMapping("/update")
    public LinkedHashMap<String,Object> updateDailyActivity(DailyActivity dailyActivity,HttpSession session)
    {
        User user=(User)  redisService.getSessionData(session.getId(),"user");
        dailyActivity.setUserAccount(user.getUserAccount());
        return dailyActivityService.updateDailyActivity(dailyActivity);
    }

    /**
     * 通过主键查找一项日常活动
     * @param dailyId 日常活动的id
     * @return dailyActivity:日常活动的信息 status:操作状态
     */
    @PostMapping("/getbyid")
    public LinkedHashMap<String,Object> getDailyActivityById(int dailyId)
    {
        return dailyActivityService.getDailyActivityById(dailyId);
    }
}
